package com.lzp.systemopt.nfc

import android.content.Intent
import android.net.wifi.WpsInfo
import android.nfc.NdefMessage
import android.nfc.NdefRecord
import android.nfc.NfcAdapter
import android.nfc.Tag
import android.nfc.tech.Ndef
import android.nfc.tech.NdefFormatable
import com.utils.LogUtils
import com.utils.toast.ToastUtils


object NfcHelper {

    private val TAG = NfcHelper::class.java.simpleName

    /**
     * [pkg] 安卓的包名，[url]ios连接，ios只识别连接
     */
    fun writeNdefPkg(intent: Intent, pkg: String, url: String) {
        val tag: Tag? = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG)
        if (tag == null) {
            LogUtils.i(TAG, "写入失败")
            return
        }
        val ndefMessage = NdefMessage(arrayOf(
                NdefRecord.createApplicationRecord(pkg),
                NdefRecord.createUri(url) //ios只支持uri这种模式
                                             ))
        val size = ndefMessage.toByteArray().size

        // 1, NDEF格式数据读取
        val ndef: Ndef? = Ndef.get(tag)
        LogUtils.i(TAG, "parseNdef, ndef=$ndef")
        if (ndef != null) {
            if (!ndef.isWritable) {
                LogUtils.i(TAG, "NFC不支持写入")
                return
            }
            try {
                ndef.connect()
                LogUtils.i(TAG, "parseNdef, ndef connected")
                LogUtils.i(TAG, "parseNdef, isWritable=${ndef.isWritable}, maxSize=${ndef.maxSize}, type=${ndef.type}")
                if (ndef.isWritable && ndef.maxSize >= size) {
                    ndef.writeNdefMessage(ndefMessage)
                    LogUtils.i(TAG, "parseNdef, ndef 写入成功")
                    ToastUtils.toastText("写入成功")
                }
            } catch (e: Exception) {
                ToastUtils.toastText("写入失败")
            } finally {
                try {
                    ndef.close()
                } catch (e2: Exception) {
                    e2.printStackTrace()
                }
            }
        }

        val ndefFormat: NdefFormatable? = NdefFormatable.get(tag)
        LogUtils.i(TAG, "parseNdef, ndefFormat=$ndefFormat")
        if (ndefFormat != null) {
            try {
                ndefFormat.connect()
                LogUtils.i(TAG, "parseNdef, ndefFormat connected")
                ndefFormat.format(ndefMessage)
                LogUtils.i(TAG, "parseNdef, ndefFormat 写入成功")
            } catch (e: Exception) {
                e.printStackTrace()
            } finally {
                try {
                    ndefFormat.close()
                } catch (e2: Exception) {
                    e2.printStackTrace()
                }
            }
        }
    }


    /**
     * 写入wifi信息
     */
    fun writeWifi(intent: Intent) {
        val tag: Tag? = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG)
        if (tag == null) {
            LogUtils.i(TAG, "写入失败")
            return
        }

        val wpsInfo = WpsInfo()
        wpsInfo.BSSID = "Niu_Guest"
        wpsInfo.setup = WpsInfo.DISPLAY

        val array = getArray()
        val ndefMessage = NdefMessage(arrayOf(NdefRecord.createMime("application/vnd.wfa.wsc", array)))
        val size = ndefMessage.toByteArray().size

        // 1, NDEF格式数据读取
        val ndef: Ndef? = Ndef.get(tag)
        LogUtils.i(TAG, "parseNdef, ndef=$ndef")
        if (ndef != null) {
            if (!ndef.isWritable) {
                LogUtils.i(TAG, "NFC不支持写入")
                return
            }
            try {
                ndef.connect()
                LogUtils.i(TAG, "parseNdef, ndef connected")
                LogUtils.i(TAG, "parseNdef, isWritable=${ndef.isWritable}, maxSize=${ndef.maxSize}, type=${ndef.type}")
                if (ndef.isWritable && ndef.maxSize >= size) {
                    ndef.writeNdefMessage(ndefMessage)
                    LogUtils.i(TAG, "parseNdef, ndef 写入成功")
                    ToastUtils.toastText("写入成功")
                }
            } catch (e: Exception) {
                ToastUtils.toastText("写入失败")
            } finally {
                try {
                    ndef.close()
                } catch (e2: Exception) {
                    e2.printStackTrace()
                }
            }
        }

        val ndefFormat: NdefFormatable? = NdefFormatable.get(tag)
        LogUtils.i(TAG, "parseNdef, ndefFormat=$ndefFormat")
        if (ndefFormat != null) {
            try {
                ndefFormat.connect()
                LogUtils.i(TAG, "parseNdef, ndefFormat connected")
                ndefFormat.format(ndefMessage)
                LogUtils.i(TAG, "parseNdef, ndefFormat 写入成功")
            } catch (e: Exception) {
                e.printStackTrace()
            } finally {
                try {
                    ndefFormat.close()
                } catch (e2: Exception) {
                    e2.printStackTrace()
                }
            }
        }
    }

    private fun getArray(): ByteArray {
        return byteArrayOf(0x10, 0x0E, 0x00, 0x3C, 0x10, 0x26, 0x00,
                           //                  E
                           0x01, 0x01, 0x10, 0x45, 0x00, 0x09,
                           //Niu-Guest ssid
                           0x4E, 0x69, 0x75, 0x2D, 0x47, 0x75, 0x65, 0x73, 0x74,
                           0x10, 0x03, 0x00, 0x02, 0x00, 0x01, 0x10, 0x0F, 0x00, 0x02, 0x00, 0x01, 0x10, 0x27, 0x00, 0x10,
                           //welcomtoxiaoniu pwd
                           0x77, 0x65, 0x6C, 0x63, 0x6F, 0x6D, 0x65, 0x74, 0x6F, 0x78, 0x69, 0x61, 0x6F, 0x6E, 0x69, 0x75,
                           0x10, 0x20, 0x00, 0x06, 0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte())
    }
}